package com.service;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ExecutionException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.atlassian.jira.rest.client.JiraRestClient;
import com.atlassian.jira.rest.client.JiraRestClientFactory;
import com.atlassian.jira.rest.client.domain.BasicIssue;
import com.atlassian.jira.rest.client.internal.async.AsynchronousJiraRestClientFactory;

@Service
public class JiraRestClientService {
	@Autowired
	private String jiraUser;
	@Autowired
	private String jiraPassword;
	@Autowired
	private String jiraUrl;

	private List<String> issuesList = new ArrayList<String>();

	public List<String> findIssuesByJql(String jql) {
		int startAt = 0;
		JiraRestClientFactory factory = new AsynchronousJiraRestClientFactory();
		JiraRestClient restClient;
		try {
			restClient = factory.createWithBasicHttpAuthentication(new URI(
					jiraUrl), jiraUser, jiraPassword);

			Iterator<BasicIssue> iterator;
			try {
				iterator = restClient.getSearchClient()
						.searchJql(jql, 1000, startAt).get().getIssues()
						.iterator();

				while (iterator.hasNext()) {
					while (iterator.hasNext()) {
						BasicIssue issue = iterator.next();
						issuesList.add(issue.getKey());
					}
					if (issuesList.size() % 1000 == 0) {
						startAt += 1000;
						iterator = restClient.getSearchClient()
								.searchJql(jql, 1000, startAt).get()
								.getIssues().iterator();
					}
				}

			} catch (InterruptedException e) {
				System.out.println("Something wrong");
				e.printStackTrace();
			} catch (ExecutionException e) {
				System.out
						.println("Can't execute jql or something wrong with Jira");
				e.printStackTrace();
			}
		} catch (URISyntaxException e) {
			System.out.println("Can't login to Jira");
			e.printStackTrace();
		}
		return issuesList;

	}

	public List<String> getAllCloseToExpireTickets() {
		return findIssuesByJql("duedate <= endOfDay()");
	}

	public List<String> getAllExpiringTicketsTillDate(String dueDate) {
		return findIssuesByJql("duedate <= " + "'" + dueDate + "'");
	}

	public List<String> getAllResolvedTickets() {
		return findIssuesByJql("status=resolved");
	}

	/**
	 * Valid formats include: 'YYYY-MM-DD'
	 * 
	 */
	public List<String> getAllTicketsBetweenDate(String fromDate, String toDate) {
		return findIssuesByJql("created >= " + "'" + fromDate + "'" + " AND "
				+ " duedate <= " + "'" + toDate + "'");
	}

	public List<String> getAllTicketsByAssignee(String assignee) {

		return findIssuesByJql("Assignee =" + "'" + assignee + "'");
	}

	public List<String> getAllUndoneTicketsByIssueType(String issueType) {

		return findIssuesByJql("issueType =" + "'" + issueType + "'"
				+ " AND status != resolved");
	}

	public List<String> getAllInProgressTicketsByAssignee(String assignee) {

		return findIssuesByJql("Assignee =" + "'" + assignee + "'"
				+ " AND status = 'IN PROGRESS'");
	}
}
